﻿using UnityEngine;
using System.Collections;

public class ChargeBehaviour : BehaviourNode {

    public float speedBoost;
    public float chargeRange;
    public float range;

    public override IEnumerator Activate()
    {
        running = true;

        if (Unit.target &&
            Vector3.Distance(Unit.target.transform.position, transform.position) < chargeRange &&
            Unit.StraightLine(Unit.target.transform.position))
        {
            var target = Unit.target;
            Vector3 tPos = target.transform.position;

            Unit.speed += speedBoost;
            float dist;
            do
            {
                Unit.path = WorldGrid.Instance.FindPath(transform.position, target.transform.position, Unit.mask);

                if (Unit.target != target || !Unit.StraightLine(target.transform.position))
                    dist = -1;

                yield return false;
                dist = Vector3.Distance(target.transform.position, transform.position);
            }
            while (dist > range && dist < chargeRange);
            Unit.speed -= speedBoost;
        }

        running = false;
    }
}
